package com.carrentalplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrentalplatform.mapper.VehicleMapper;
import com.carrentalplatform.model.Vehicle;
import com.carrentalplatform.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 车辆服务实现类
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public IPage<Vehicle> pageAvailableVehicles(int page, int size, String brand, String model, 
                                               Integer minPrice, Integer maxPrice) {
        Page<Vehicle> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Vehicle> queryWrapper = new LambdaQueryWrapper<>();
        
        // 只查询可租状态的车辆
        queryWrapper.eq(Vehicle::getStatus, 1);
        
        // 条件过滤
        if (StringUtils.hasText(brand)) {
            queryWrapper.like(Vehicle::getBrand, brand);
        }
        if (StringUtils.hasText(model)) {
            queryWrapper.like(Vehicle::getModel, model);
        }
        if (minPrice != null) {
            queryWrapper.ge(Vehicle::getDailyPrice, minPrice);
        }
        if (maxPrice != null) {
            queryWrapper.le(Vehicle::getDailyPrice, maxPrice);
        }
        
        // 按价格排序
        queryWrapper.orderByAsc(Vehicle::getDailyPrice);
        
        return this.page(pageParam, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateVehicleStatus(Long id, Integer status) {
        Vehicle vehicle = this.getById(id);
        if (vehicle == null) {
            return false;
        }
        
        vehicle.setStatus(status);
        return this.updateById(vehicle);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateVehicleLocation(Long id, String location) {
        Vehicle vehicle = this.getById(id);
        if (vehicle == null) {
            return false;
        }
        
        vehicle.setCurrentLocation(location);
        return this.updateById(vehicle);
    }

    @Override
    public List<Vehicle> listByShopId(Long shopId) {
        LambdaQueryWrapper<Vehicle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vehicle::getShopId, shopId);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addVehicle(Vehicle vehicle) {
        // 如果未设置状态，默认为可租状态
        if (vehicle.getStatus() == null) {
            vehicle.setStatus(1);
        }
        
        return this.save(vehicle);
    }

    @Override
    public Vehicle getVehicleDetail(Long id) {
        return this.getById(id);
    }
    
    @Override
    public List<Vehicle> getAllVehicles() {
        return this.list();
    }
    
    @Override
    public Vehicle findById(Long id) {
        return this.getById(id);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateVehicle(Vehicle vehicle) {
        Vehicle existingVehicle = this.getById(vehicle.getId());
        if (existingVehicle == null) {
            return false;
        }
        
        return this.updateById(vehicle);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteVehicle(Long id) {
        return this.removeById(id);
    }
} 